<template>
	<view class="u-page">

		<u--form :borderBottom='true' labelPosition='left' labelWidth='auto' :model="formState">



			<u-form-item :borderBottom='true' borderBottom label="整改原因" prop="name">
				<u-textarea v-model="formState.Reason"></u-textarea>
			</u-form-item>

			<u-form-item :borderBottom='true' borderBottom label="整改时限(天)" prop="name">
				<u-input type="number" v-model="formState.DayLimit"></u-input>
			</u-form-item>
			<u-collapse :value="['1','2','3','4']">

				<u-collapse-item name="1" title="需要整改照片" icon="https://cdn.uviewui.com/uview/example/image.png">
					<u-upload :sizeType='["compressed"]' :fileList="fileList1" @afterRead="afterRead"
						@delete="deletePic" name="1" multiple :maxCount="10"></u-upload>
				</u-collapse-item>

				<u--text margin="0 0 8px 0" type="info" prefixIcon="map" :text="Addr">
				</u--text>

			</u-collapse>
		</u--form>

		<u-button type="primary" text="提交" @click="save"></u-button>
		<u-notify ref="uNotify"></u-notify>
		<u-loading-page :loading="loading" loading-text="正在定位" bg-color='rgba(0, 0, 0, 0.3)'></u-loading-page>
	</view>
</template>

<script>
	import coordtransform from 'coordtransform'
	import {
		baseUrl
	} from '@/common/config.js'


	import {
		getbuildlist,
		addbuildputright,
		GetAddrByLatlng
	} from '@/common/api.js'
	import {
		
		getbuildbyid,
		GetNextOption,
		GoNext,
		GetCurrentAct,
	} from '@/common/api.js'
	export default {
		data() {
			return {
				nextid:'2b9a81e9-b2fd-44be-aac7-6afab064092e',
				CheckJsonArr: [],
				CheckJson: '1111111111111111111',
				current: 0,
				loading: false,
				build:{},

				user: null,
				token: null,
				// Addr:"",
				formState: {},
				Addr: "",
				fileList1: [],
				fileList2: [],
				buildid: null,
				id: null,
				notifyData: {
					message: '保存成功',
					type: 'success',
					color: '#ffffff',
					bgColor: '',
					fontSize: 15,
					duration: 3000,
					safeAreaInsetTop: false
				},
			}
		},
		onLoad(option) {
			this.user = JSON.parse(uni.getStorageSync("$userinfo"))
			this.token = uni.getStorageSync("$token")
			this.CheckJsonArr = this.CheckJson.split('')
			this.buildid = option.buildid
			this.outlocation()
			if (option.buildid)
				this.getdate(option.buildid)
		},


		methods: {
			outlocation() {

				uni.getLocation({
					type: 'wgs84',
					geocode: true, //设置该参数为true可直接获取经纬度及城市信息
					success: (res) => {
						console.log("定位结果" + res.longitude + "，" + res.latitude)
						// that.addrDel = res;
						this.outcenter = [res.longitude, res.latitude]
						//  this.outcenter = [parseFloat(res.longitude), parseFloat(res.latitude)]
						// this.outcenter = [111.11111, 11.111111]
						// this.view.goTo({
						// 	center: [res.longitude, res.latitude],
						// 	zoom: 15
						// });
						// this.outcenter = [119,24]
						// console.log(res.longitude)
						this.formState.Latlng = res.latitude + ',' + res.longitude
						var gcj02 = coordtransform.wgs84togcj02(res.longitude, res.latitude)
						console.log(gcj02);
						GetAddrByLatlng(gcj02[1] + "," + gcj02[0]).then(res2 => {
							console.log(res2);
							this.Addr = res2;
							// this.Addr=res2

						})


						// this.formState.Addr = res2.result.formatted_addresses
						this.loading = false
					},
					fail: () => {
						// this.outcenter = [111.11111, 11.111111]
						// console.log("定位结果失败")
						// console.log(this)
						uni.showToast({
							title: '获取地址失败，将导致部分功能不可用',
							icon: 'none'
						});
					}
				});
				// this.outcenter = [119,24]
			},
			save() {
				this.formState.BuildId = this.buildid
				this.formState.Status = "未完成"
				addbuildputright(this.formState).then(res => {
					console.log(this.build)
					GoNext(this.nextid, "通过", this.build.ProjManagerId, res.Reason, "5", res.Id, this
						.build.Name, null).then(res => {
						// if (res.status == 200) {
							// var route = "/dashboard/todo"
							// this.setfresshroute(route);
							// this.$message.success('发送成功。');
							// this.Visible = false
							// this.isDataLoaded = false
						// }
						// this.confirmLoading = false
						// uni.navigateBack()
						// this.initAll()
						// if(this.cometype=="通过" && res.data.FlowAction.WriteBack)
						// {
						// let data = this.formstate;
						// console.log(this.formstate+"主体")
						// eval(res.data.FlowAction.WriteBack)
						// this.$emit("writeback",data)//回写主体
						// }					
						this.$refs.uNotify.show(this.notifyData)
						uni.setStorageSync("changed", "1")

						uni.navigateTo({
							url: "list"
						})
					})


				})

			},
			getdate(id) {
				getbuildbyid(id).then(res => {
					this.build = res
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url, '要求整改照片')
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				// console.log(this.user.Id)
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: `${baseUrl}/SysFile/upload?outid=${this.formState.Id||this.user.Id}&type=要求整改照片`,
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						header: {
							"Authorization": `Bearer ${this.token}`
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style>

</style>